package com.sidneysimmons.financially.managers;

import java.util.ArrayList;

import android.content.Context;
import android.widget.Toast;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.adapters.DatabaseAdapter;
import com.sidneysimmons.financially.exceptions.DuplicateCategoryException;

public class CategoryManager {
	
	private Context context;
	
	/**
	 * Utility class for all helper methods having to do with categories.
	 */
	public CategoryManager(Context context) {
		this.context = context;
	}
	
	/**
	 * Adds a new Category to the device database via the DatabaseAdapter. This assumes 
	 * the Category has already successfully passed validation.
	 * 
	 * @param account - the account this category is in
	 * @param newCategory - the category to add
	 */
	public boolean addCategory(Account account, Category newCategory) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		try {
			successful = database.addCategory(account.getTitle(), newCategory);
		} catch (DuplicateCategoryException e) {
			// A Category with the same name already exists in the database
			String errorText = context.getResources().getString(R.string.exception_duplicate_category);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
		}
		
		return successful;
	}
	
	/**
	 * Delete a category from the device's database.
	 * 
	 * @param accountName
	 * @param category
	 */
	public boolean deleteCategory(Account account, Category category) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		successful = database.deleteCategory(account.getTitle(), category.getTitle());
		
		if (!successful) {
			String errorText = context.getResources().getString(R.string.edit_category_dialog_text_delete_error);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
		}
		
		return successful;
	}
	
	/**
	 * Edit the category in the device's database.  This assumes that the updated category has 
	 * passed front end validation.  Returns true if successful, false otherwise.
	 * 
	 * @param account
	 * @param originalCategory
	 * @param updatedCategory
	 */
	public boolean editCategory(Account account, Category originalCategory, Category updatedCategory) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		try {
			successful = database.updateCategory(account.getTitle(), originalCategory, updatedCategory);
		} catch (DuplicateCategoryException e) {
			// A Category with the same name already exists in the database
			String errorText = context.getResources().getString(R.string.exception_duplicate_category);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
		}
		
		return successful;
	}
	
	/**
	 * Returns ArrayList<Category> of all categories.
	 * 
	 * @param accountName - get categories from this account
	 */
	public ArrayList<Category> getCategories(String accountName) {
		// Get the categories from the device database
		DatabaseAdapter database = new DatabaseAdapter(context);
		ArrayList<Category> categories = database.getCategories(accountName);
		
		return categories;
	}
	
	/**
	 * Returns all the categories in an array versus an ArrayList.
	 * 
	 * @param accountName - get categories from this account
	 */
	public Category[] getCategoriesArray(String accountName) {
		ArrayList<Category> categories = getCategories(accountName);
		
		// Put categories in an array, instead of an ArrayList
		Category[] categoryArray = new Category[categories.size()];
		for (int i = 0; i < categories.size(); i++) {
			categoryArray[i] = categories.get(i);
		}
		
		return categoryArray;
	}
}